﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace TradeForm
{
    public partial class FrmBillBrowser : Form
    {
        System.Windows.Forms.DataGridView.HitTestInfo htInfo = null;


        DataDBDataContext dbcntxt = new DataDBDataContext
                (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);

        public FrmBillBrowser()
        {
            InitializeComponent();
        }

        private void FrmBillBrowser_Load(object sender, EventArgs e)
        {
            // BindData();
            this.dataGridView1.DataSource = null;
            this.dataGridView1.AutoGenerateColumns = false;
            var bills = (from cont in dbcntxt.Contract
                         join inv in dbcntxt.Invoice
                             on cont.DefCode equals inv.DefCode
                         orderby inv.InvoiceDate descending
                         select new { cont.DefCode, cont.ContractNo, inv.InvoiceNo }).Take(50)
                        ;

            this.dataGridView1.DataSource = bills;
        }

        protected void BindData()
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.AutoGenerateColumns = false;
            var bills = from cont in dbcntxt.Contract
                        join inv in dbcntxt.Invoice
                            on cont.DefCode equals inv.DefCode
                        select new { cont.DefCode, cont.ContractNo, inv.InvoiceNo };

            this.dataGridView1.DataSource = bills;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1[e.ColumnIndex, e.RowIndex].GetType() == typeof(System.Windows.Forms.DataGridViewLinkCell))
            {
                DataGridViewLinkCell cell = (DataGridViewLinkCell)this.dataGridView1[e.ColumnIndex, e.RowIndex];
                //MessageBox.Show(cell.Value.ToString());
                FrmTradeForm frm = new FrmTradeForm(cell.Value.ToString());
                frm.MdiParent = this.MdiParent;
                frm.Show();
                this.Close();
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.AutoGenerateColumns = false;
            var bills = from cont in dbcntxt.Contract
                        join inv in dbcntxt.Invoice
                            on cont.DefCode equals inv.DefCode
                        where cont.DefCode.Contains(this.txttxtSrchDefCode.Text)
                        && inv.InvoiceNo.Contains(this.txtSrchInvoiceNo.Text)
                        && cont.ContractNo.Contains(this.txtSrchContractNo.Text)
                        select new { cont.DefCode, cont.ContractNo, inv.InvoiceNo };

            this.dataGridView1.DataSource = bills;
            // BindData();
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            htInfo = dataGridView1.HitTest(e.X, e.Y);
            if (e.Button == MouseButtons.Right)
            {

                if (this.htInfo != null && htInfo.Type == DataGridViewHitTestType.Cell)
                {
                    this.dataGridView1.ClearSelection();
                    this.dataGridView1.Rows[htInfo.RowIndex].Selected = true;
                    contextMenu.Show(this.dataGridView1, e.X, e.Y);
                }
            }
        }

        private void toolMenuOpen_Click(object sender, EventArgs e)
        {
            if (htInfo != null && htInfo.Type == DataGridViewHitTestType.Cell)
            {
                if (this.dataGridView1[0, htInfo.RowIndex].GetType() == typeof(System.Windows.Forms.DataGridViewLinkCell))
                {
                    DataGridViewLinkCell cell = (DataGridViewLinkCell)this.dataGridView1[0, htInfo.RowIndex];

                    FrmTradeForm frm = new FrmTradeForm(cell.Value.ToString());
                    frm.MdiParent = this.MdiParent;
                    frm.Show();
                    this.Close();
                }
            }

        }

        private void toolMenuDel_Click(object sender, EventArgs e)
        {
            if (htInfo != null && htInfo.Type == DataGridViewHitTestType.Cell)
            {
                if (this.dataGridView1[htInfo.ColumnIndex, htInfo.RowIndex].GetType() == typeof(System.Windows.Forms.DataGridViewLinkCell))
                {
                    if (MessageBox.Show("是否要删除该记录？", "请确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        DataGridViewLinkCell cell = (DataGridViewLinkCell)this.dataGridView1[0, htInfo.RowIndex];
                        string defcode = cell.Value.ToString();

                        #region //BillExchange
                        var billexs = from bill in dbcntxt.BillExchange
                                      where bill.DefCode == defcode
                                      select bill;
                        if (billexs.Count() > 0)
                        {
                            dbcntxt.BillExchange.DeleteAllOnSubmit(billexs.AsEnumerable());
                        }
                        #endregion

                        #region //ProFormaInvoice
                        var pinvs = from bill in dbcntxt.ProFormaInvoice
                                    where bill.DefCode == defcode
                                    select bill;
                        if (pinvs.Count() > 0)
                        {
                            dbcntxt.ProFormaInvoice.DeleteAllOnSubmit(pinvs.AsEnumerable());
                        }
                        #endregion

                        #region //ProFormaInvoiceGoods
                        var pinvGoodss = from bill in dbcntxt.ProFormaInvoiceGoods
                                         where bill.DefCode == defcode
                                         select bill;
                        if (pinvGoodss.Count() > 0)
                        {
                            dbcntxt.ProFormaInvoiceGoods.DeleteAllOnSubmit(pinvGoodss.AsEnumerable());
                        }
                        #endregion

                        #region //Invoice
                        var invs = from bill in dbcntxt.Invoice
                                   where bill.DefCode == defcode
                                   select bill;
                        if (invs.Count() > 0)
                        {
                            dbcntxt.Invoice.DeleteAllOnSubmit(invs.AsEnumerable());
                        }
                        #endregion

                        #region //Contract
                        var conts = from bill in dbcntxt.Contract
                                    where bill.DefCode == defcode
                                    select bill;
                        if (conts.Count() > 0)
                        {
                            dbcntxt.Contract.DeleteAllOnSubmit(conts.AsEnumerable());
                        }
                        #endregion

                        #region //CustomsDeclaration
                        var custs = from bill in dbcntxt.CustomsDeclaration
                                    where bill.DefCode == defcode
                                    select bill;
                        if (custs.Count() > 0)
                        {
                            dbcntxt.CustomsDeclaration.DeleteAllOnSubmit(custs.AsEnumerable());
                        }
                        #endregion

                        #region //ConsignBill
                        var consbs = from bill in dbcntxt.ConsignBill
                                     where bill.DefCode == defcode
                                     select bill;
                        if (consbs.Count() > 0)
                        {
                            dbcntxt.ConsignBill.DeleteAllOnSubmit(consbs.AsEnumerable());
                        }
                        #endregion

                        #region //PackList
                        var packs = from bill in dbcntxt.PackList
                                    where bill.DefCode == defcode
                                    select bill;
                        if (packs.Count() > 0)
                        {
                            dbcntxt.PackList.DeleteAllOnSubmit(packs.AsEnumerable());
                        }
                        #endregion

                        #region //PackListGoods
                        var packgoods = from bill in dbcntxt.PackListGoods
                                        where bill.DefCode == defcode
                                        select bill;
                        if (packgoods.Count() > 0)
                        {
                            dbcntxt.PackListGoods.DeleteAllOnSubmit(packgoods.AsEnumerable());
                        }
                        #endregion

                        #region //GoodsSummery
                        var goodsSums = from bill in dbcntxt.GoodsSummery
                                        where bill.DefCode == defcode
                                        select bill;
                        if (goodsSums.Count() > 0)
                        {
                            dbcntxt.GoodsSummery.DeleteAllOnSubmit(goodsSums.AsEnumerable());
                        }
                        #endregion

                        #region //GoodsDetail
                        var goods = from good in dbcntxt.GoodsDetail
                                    where good.DefCode == defcode
                                    select good;
                        if (goods.Count() > 0)
                        {
                            dbcntxt.GoodsDetail.DeleteAllOnSubmit(goods.AsEnumerable());
                        }
                        #endregion


                        dbcntxt.SubmitChanges();
                        BindData();

                    }

                }
            }
        }

        private void ToolMenuCopy_Click(object sender, EventArgs e)
        {
            if (htInfo != null && htInfo.Type == DataGridViewHitTestType.Cell)
            {
                if (this.dataGridView1[0, htInfo.RowIndex].GetType() == typeof(System.Windows.Forms.DataGridViewLinkCell))
                {
                    DataGridViewLinkCell cell = (DataGridViewLinkCell)this.dataGridView1[0, htInfo.RowIndex];

                    FrmTradeForm frm = new FrmTradeForm(cell.Value.ToString(), true);
                    frm.MdiParent = this.MdiParent;
                    frm.Show();
                    this.Close();
                }
            }
        }
    }
}
